package com.example.springquerydslsample2;

import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import com.example.springquerydslsample2.QCar;

import java.util.List;
import java.util.function.Consumer;

@RestController
@RequestMapping
public class CarController {
    Logger log = LoggerFactory.getLogger(CarController.class);

    @Autowired
    JPAQueryFactory factory;

    @Autowired
    CarRepository repository;
    @RequestMapping(value = {"addCar1"}, method = {RequestMethod.POST})
    public ResponseResult addCar1(@RequestBody Car car) {

        repository.save(car);
        return new ResponseResult("添加成功");
    }

    @RequestMapping(value = {"getCar"},method = {RequestMethod.GET})
    public ResponseResult getCar(){
        QCar qCar = QCar.car;
        List<Car> qu = factory.selectFrom(qCar).where(qCar.name.eq("比亚迪")).fetch();//查询名字等于“比亚迪”的数据
        qu.forEach(new Consumer<Car>() {
            @Override
            public void accept(Car car) {
                log.debug(car.getName());
            }
        });
        Car car = qu.get(0);
        return new ResponseResult(car.getName());//返回Car的名字
    }
}
